home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / grafik / cgazv5n3 / main1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-06  |  4.4 KB  |  137 lines

  1. /********* Listing 5 *************************** MAIN1.CPP *****
  2. *  Tests dynamic hasher. Reads any text file and inserts words 
  3. *  into table. Allows user to insert, remove and retrieve words 
  4. *  from hash table.
  5. ****************************************************************/
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include <stream.hpp>
  11. #include "dynhash.hpp"
  12.  
  13. struct REC {
  14.     char word[64];              // record key
  15.     int number;                 // key number
  16. };
  17.  
  18. REC item;
  19. int number;
  20. declare(gdynhash,REC);
  21. gdynhash(REC) HashTable;     // declare dynamic hash table
  22.  
  23. main( int argc, char* argv[] )
  24. {
  25.     FILE *fp;
  26.     char *p, *q, buf[84];
  27.     int eol, i, n=1;
  28.     int choice, index;
  29.     if (argc != 2)
  30.     {
  31.         cout << "Provide a text file name as argument\n";
  32.         exit(1);
  33.     }
  34.     fp = fopen(argv[1],"r");
  35.     if (!fp)
  36.     {
  37.         cout << "file not found\n";
  38.         exit(1);
  39.     }
  40.     cout << "Put unique words into hash table\n";
  41.     while (fgets(buf, 80, fp))
  42.     {
  43.         for (eol = 0, p = buf; *p;)
  44.         {
  45.             while (*p && !isalpha(*p))
  46.                 ++p;
  47.             if (!*p) break;
  48.             for (q = p; isalpha(*q); ++q)
  49.                 ;
  50.             if (!*q) ++eol;
  51.             *q = '\0';
  52.             strlwr(p);
  53.             i = strlen(p);
  54.             if (i)
  55.             {            // found a word
  56.                 strcpy( item.word, p );
  57.                 item.number = i;        // use strlen as 'number'
  58.                 index = HashTable.insert( &item );
  59.                 int err = HashTable.error();
  60.                 if ( err == NO_MEMORY || err == EXPAND_ERROR )
  61.                 {
  62.                   cout << "\n\nFile too big, only " << HashTable.size()
  63.                   << " records inserted\n";
  64.                   exit(1);
  65.                 }
  66.             }
  67.             if (eol)
  68.                 break;
  69.             p = q+1;
  70.         }
  71.     }
  72.  
  73.     for ( ;; )
  74.     {
  75.       int out=0;                        // for exiting
  76.  
  77.       cout << "\nEnter choice\n";
  78.       cout << "\t(1) Database size\n";
  79.       cout << "\t(2) Show all keys\n";
  80.       cout << "\t(3) Insert item\n";
  81.       cout << "\t(4) Remove item\n";
  82.       cout << "\t(5) Lookup item\n";
  83.       cout << "\t(6) Quit\n";
  84.       cout << "\n\t?: ";
  85.       cin >> choice;
  86.  
  87.       if ( choice < 1 && choice > 6 )
  88.         cout << "\nChoice must be between 1 and 6\n";
  89.       else
  90.         switch ( choice )
  91.         {
  92.           case 1 : 
  93.              cout << "\nDatabase size = " <<HashTable.size()<< " records";
  94.                    break;
  95.           case 2 : char *ptr;       // dump all hash table keys
  96.                    for ( i=0; i < HashTable.size(); )
  97.                        if ( (ptr = (char *) HashTable.get_next()) != NULL )
  98.                         cout << "\n" << ++i << ") " << ptr;
  99.                    break;
  100.           case 3 : cout << "\nKey to insert: ";
  101.                    cin >> item.word;
  102.                    cout << "Number for this record: ";
  103.                    cin >> item.number;
  104.                    index = HashTable.insert( &item );
  105.                    if ( index > 0 )
  106.                       cout << "\nItem inserted at index = " << index;
  107.                    else if ( index == NO_MEMORY )
  108.                       cout << "\nCan't insert - OUT OF MEMORY";
  109.                    else if ( index == NOT_INSERTED )
  110.                       cout << "\nItem already in table";
  111.                    break;
  112.           case 4 : cout << "\nKey to remove: ";
  113.                    cin >> item.word;
  114.                    index = HashTable.remove( &item );
  115.                    if ( index >= 0 )
  116.                      cout << "\nItem removed at index = " << index;
  117.                    else
  118.                      cout << "\nItem not in table\n";
  119.                    break;
  120.           case 5 : cout << "\nKey to lookup: ";
  121.                    cin >> item.word;
  122.                    REC *pt = HashTable.lookup( &item );
  123.                    if ( pt )
  124.                    {
  125.                      cout << "\nKey found = " << pt->word;
  126.                      cout << "\nRec number= " << pt->number;
  127.                    }
  128.                    else
  129.                      cout << "\nKey not found in table\n";
  130.                    break;
  131.           case 6 : cout << "\nBye ...\n";
  132.                    out = 1;
  133.                    break;
  134.         }
  135.        if ( out ) break;
  136.      }
  137. }